#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../inc/student.h"
#include "../inc/admin.h"
#include "../inc/general.h"

// 初始化学生信息
Student_Node_p student_Init()
{
    Student_Node_p stu_head = studentNode_Create();

    // 学生数据数组
    Student_t students[15] = {
        {"001", "张伟", "GZ2528", 20, 1},
        {"002", "王芳", "GZ2528", 21, 0},
        {"003", "李娜", "GZ2528", 22, 1},
        {"004", "刘强", "GZ2529", 20, 0},
        {"005", "陈静", "GZ2528", 21, 1},
        {"006", "杨光", "GZ2528", 22, 0},
        {"007", "赵敏", "GZ2528", 20, 1},
        {"008", "黄磊", "GZ2528", 21, 0},
        {"009", "周雪", "GZ2529", 22, 1},
        {"010", "张伟", "GZ2529", 20, 0},
        {"011", "徐丽", "GZ2529", 21, 1},
        {"012", "孙浩", "GZ2528", 22, 0},
        {"013", "马婷", "GZ2529", 20, 1},
        {"014", "朱涛", "GZ2529", 21, 0},
        {"015", "胡月", "GZ2529", 22, 1}};

    Student_Node_p prev = NULL;
    for (int i = 0; i < 15; i++)
    {
        Student_Node_p node = studentNode_Create();
        if (!node)
            continue;

        // 填充数据
        strcpy(node->data->id, students[i].id);
        strcpy(node->data->name, students[i].name);
        strcpy(node->data->class, students[i].class); // 统一班级
        node->data->age = students[i].age;
        node->data->sex = students[i].sex;

        // 链接节点
        if (i == 0)
        {
            stu_head->next = node;
        }
        else
        {
            prev->next = node;
            node->prev = prev;
        }
        prev = node;
    }

    return stu_head;
}

// 创建学生节点
Student_Node_p studentNode_Create()
{
    Student_Node_p p = (Student_Node_p)malloc(sizeof(Student_Node_t));
    p->data = (Student_p)malloc(sizeof(Student_t));
    p->next = NULL;
    p->prev = NULL;
    return p;
}

// 查找学生节点
Student_Node_p find_studentNode(Student_Node_p stu_head, int index)
{
    Student_Node_p p = stu_head->next;
    if (index < 0)
    {
        return NULL;
    }
    while (index > 0)
    {
        if (p == NULL)
        {
            return NULL;
        }
        else
        {
            index--;
            p = p->next;
        }
    }
    return p;
}

// 通过学号获取对应学生索引
int getStudentIndex_ByID(Student_Node_p stu_head, char *id)
{
    int index = 0;
    Student_Node_p p = stu_head->next;
    while (p != NULL)
    {
        if (strcmp(p->data->id, id) == 0)
        {
            return index;
        }
        index++;
        p = p->next;
    }
    return -1;
}

// 统计学生节点个数
int stuCount(Student_Node_p stu_head)
{
    Student_Node_p p = stu_head->next;
    int count = 0;
    while (p != NULL)
    {
        count++;
        p = p->next;
    }
    return count;
}

// 插入学生节点（指定位置）
void insert_studentNode(Student_Node_p stu_head, int index)
{
    if (index < 0 || index > stuCount(stu_head))
    {
        printf("index error\n");
        return;
    }
    Student_Node_p currNode = find_studentNode(stu_head, index);
    Student_Node_p newNode = studentNode_Create();
    newNode = add_studentInfo(stu_head, newNode);
    if (currNode == NULL)
    {
        if (stuCount(stu_head) == 0)
        {
            stu_head->next = newNode;
        }
        else
        {
            Student_Node_p lastNode = find_studentNode(stu_head, stuCount(stu_head) - 1);
            lastNode->next = newNode;
            newNode->prev = lastNode;
        }
    }
    else
    {
        Student_Node_p prevNode = currNode->prev;
        if (prevNode == NULL)
        {
            stu_head->next = newNode;
            newNode->next = currNode;
            currNode->prev = newNode;
        }
        else
        {
            newNode->next = currNode;
            newNode->prev = prevNode;
            prevNode->next = newNode;
            currNode->prev = newNode;
        }
    }
}

// 插入学生节点（尾插法）
void insertTail_studentNode(Student_Node_p stu_head)
{
    insert_studentNode(stu_head, stuCount(stu_head));
}

// 删除学生节点
int delete_studentNode(Student_Node_p stu_head, int index)
{
    if (index < 0 || index >= stuCount(stu_head))
    {
        printf("删除失败，索引超出范围\n");
        return -1;
    }

    Student_Node_p currNode = find_studentNode(stu_head, index);
    Student_Node_p prevNode = currNode->prev;
    Student_Node_p nextNode = currNode->next;
    if (prevNode == NULL)
    {
        if (nextNode == NULL)
        {
            stu_head->next = NULL;
        }
        else
        {
            stu_head->next = nextNode;
            nextNode->prev = NULL;
        }
    }
    else
    {
        if (nextNode == NULL)
        {
            prevNode->next = NULL;
        }
        else
        {
            prevNode->next = nextNode;
            nextNode->prev = prevNode;
        }
    }
    free(currNode->data);
    free(currNode);
    return 1;
}

// 更新学生节点
Student_Node_p updata_studentNode(Student_Node_p stu_head, int index)
{
    Student_Node_p currNode = find_studentNode(stu_head, index);
    if (currNode == NULL)
    {
        printf("更新失败，索引超出范围\n");
        return NULL;
    }
    scanf_studentInfo(stu_head, currNode);
}

// 打印所有学生信息
void print_ALLStudentInfo(Student_Node_p stuNode)
{
    Student_Node_p p = stuNode->next;
    printf("学号\t姓名\t年龄\n");
    while (p != NULL)
    {
        printf("%s\t%s\t%d\n", p->data->id, p->data->name, p->data->age);
        p = p->next;
    }
}

// 输入学生信息
Student_Node_p scanf_studentInfo(Student_Node_p stu_head, Student_Node_p stuNode)
{
    Student_p newData = (Student_p)malloc(sizeof(Student_t));
    printf("请输入学生学号、姓名、班级、年龄、性别(男1/女0):\n");
    scanf(" %19s %19s %19s %d %d", newData->id, newData->name, newData->class, &newData->age, &newData->sex);
    while (getchar() != '\n')
        ;
    if (newData->id != stuNode->data->id)
    {
        while (getStudentIndex_ByID(stu_head, newData->id) != -1 && strcmp(newData->id, stuNode->data->id) != 0)
        {
            printf("学号重复,请重新输入学号:");
            scanf("%19s", newData->id);
            while (getchar() != '\n')
                ;
        }
    }
    while (newData->sex != 1 && newData->sex != 0)
    {
        printf("性别输入错误,请重新输入(男1/女0):");
        scanf("%d", &newData->sex);
        while (getchar() != '\n')
            ;
    }
    free(stuNode->data);
    stuNode->data = newData;
    return stuNode;
}

// 输入新增学生信息
Student_Node_p add_studentInfo(Student_Node_p stu_head, Student_Node_p stuNode)
{
    Student_p newData = (Student_p)malloc(sizeof(Student_t));
    printf("请输入学生学号、姓名、班级、年龄、性别(男1/女0):\n");
    scanf(" %19s %19s %19s %d %d", newData->id, newData->name, newData->class, &newData->age, &newData->sex);
    while (getchar() != '\n')
        ;
    while (getStudentIndex_ByID(stu_head, newData->id) != -1)
    {
        printf("学号重复,请重新输入学号:");
        scanf(" %19s", newData->id);
        while (getchar() != '\n')
            ;
    }
    while (newData->sex != 1 && newData->sex != 0)
    {
        printf("性别输入错误,请重新输入(男1/女0):");
        scanf(" %d", &newData->sex);
    }
    free(stuNode->data);
    stuNode->data = newData;
    return stuNode;
}

// 根据学生姓名查找并统计该学生个数
int getStuCountByName(Student_Node_p stu_head, char *name)
{
    Student_Node_p p = stu_head->next;
    int count = 0;
    while (p != NULL)
    {
        if (strcmp(p->data->name, name) == 0)
        {
            count++;
        }
        p = p->next;
    }
    return count;
}

// 根据学生姓名查找并打印该学生信息
void print_StudentNodeByName(Student_Node_p stu_head, char *name)
{
    Student_Node_p p = stu_head->next;
    while (p != NULL)
    {
        if (strcmp(p->data->name, name) == 0)
        {
            printf("%s\t%s\t%s\t%d\t%s\n", p->data->id, p->data->name, p->data->class, p->data->age, getSex(p->data->sex));
        }
        p = p->next;
    }
}

// 根据班级查找统计该班级学生个数
int getStuCountByClass(Student_Node_p stu_head, char *class)
{
    Student_Node_p p = stu_head->next;
    int count = 0;
    while (p != NULL)
    {
        if (!strcmp(class, p->data->class))
        {
            count++;
        }
        p = p->next;
    }
    return count;
}